package com.bishe.psy.service.impl;

import com.bishe.psy.common.AppResult;
import com.bishe.psy.common.ResultCode;
import com.bishe.psy.dao.ArticleMapper;
import com.bishe.psy.exception.ApplicationException;
import com.bishe.psy.model.Article;
import com.bishe.psy.model.ConUser;
import com.bishe.psy.model.LikeArticle;
import com.bishe.psy.service.IArticleService;
import com.bishe.psy.service.IConUserService;
import com.bishe.psy.service.ILikeArticleService;
import com.bishe.psy.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * Created with Intellij IDEA.
 * Description: 新增帖子
 * User: L
 * Date: 2024-04-10
 * Time: 11:44
 */

@Slf4j
@Service
public class ArticleServiceImpl implements IArticleService {

    @Resource
    private ArticleMapper articleMapper;

    @Resource
    private IConUserService iConUserService;


    @Override
    public void create(Article article) {

        //1.参数校验
        if(article == null || article.getUserId() == null
        || StringUtil.isEmpty(article.getTitle()) || StringUtil.isEmpty(article.getContent())) {
            log.warn("发帖操作" + ResultCode.FAILED_PARAMS_VALIDATE);
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        //2.设置默认值
        article.setReplyCount(0);
        article.setLikeCount(0);
        article.setVisitCount(0);
        article.setDeleteState((byte) 0);
        Date date = new Date();
        article.setCreateState(date);
        article.setUpdateState(date);

        //3.将数据插入到数据库中
        int row = articleMapper.insertSelective(article);
        System.out.println("++++++:"+ row);
        //4.判断是否从插入成功
        if(row <= 0 ) {
            log.warn("帖子插入" + ResultCode.FAILED);
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }

        //5.插入成功，同时更新该用户的发帖数量
        iConUserService.addOneArticleCountById(article.getUserId());

        //6.走在这里，说明两个sql语句都成功，有一个sql语句失败都不能够走到这一行
        log.info("帖子插入成功以及用户发帖数量更新成功");

        //测试事务是否回滚
       // throw new ApplicationException("测试事务是否回滚");
    }

    @Override
    public List<Article> selectAll() {

        //1.查询得到文章表中全部的文章
        List<Article> articles = articleMapper.selectAll();

        /*
        for(int i = 0; i < articles.size(); i++) {
            //System.out.println(articles.get(i));
            //根据文章的id获取发布者的信息
            Long userId = articles.get(i).getUserId();
            //System.out.println(userId);
            List<ConUser> conUsers = iConUserService.selectByArticleKey(userId);
        }*/
        return articles;
    }

    @Override
    public List<Article> selectConByIsReign() {
        List<Article> articles = articleMapper.selectConByIsReign();
        return articles;
    }

    @Override
    public Article selectArticleById(Long id) {

        //1.参数校验
        if(id == null || id <= 0) {
            log.warn("这篇id文章" + ResultCode.FAILED_NOT_EXISTS);
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_NOT_EXISTS));
        }

        //2.调用数据库
        Article article = articleMapper.selectArticleById(id);
        //System.out.println(article);


        //3.参数校验
        if (article == null) {
            log.warn("根据id查询的文章" + ResultCode.FAILED_PARAMS_VALIDATE);
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        //4.参数校验成功，需要更新该片文章的访问数量
        Article article1 = new Article();
        article1.setId(article.getId());
        article1.setVisitCount(article.getVisitCount() + 1);


        //5.将更新的数据写入数据库中
        int row = articleMapper.updateByPrimaryKeySelective(article1);
        //System.out.println(row);
        if(row != 1) {
            log.warn("文章访问数量更新" + ResultCode.FAILED);
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }

        //6. 由于读取的还是以前的visitCount数量，因为visitCount数量改变，此时需要重新设置一下visitCount数量
        article.setVisitCount(article.getVisitCount() + 1);
        return article;
    }

    @Override
    public Article selectConById(Long id) {

        //1.参数校验
        if(id == null || id <= 0) {
            log.warn(ResultCode.FAILED_USER_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_USER_NOT_EXISTS));
        }

        //2.调用数据库
        Article article = articleMapper.selectConById(id);

        //3.参数校验
        if (article == null) {
            log.warn("根据id查询的用户" + ResultCode.FAILED_PARAMS_VALIDATE);
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        //4.返回参数
        return article;
    }

    @Override
    public void modifyArticle(Long id, String title, String content) {

        //1.参数非空校验
        if(id == null || id <= 0 || StringUtil.isEmpty(title) || StringUtil.isEmpty(content)) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        //2.更新帖子对象
        Article article = new Article();
        article.setId(id);
        article.setTitle(title);
        article.setContent(content);
        article.setUpdateState(new Date());

        //3.调用数据可
        int row = articleMapper.updateByPrimaryKeySelective(article);

        //4.检查更新是否成功
        if(row != 1) {
            log.warn(ResultCode.FAILED.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }

    }

    @Override
    public Article selectByPrimaryKey(Long id) {
        //1.参数非空校验
        if(id == null || id <= 0 ) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        //2.调用数据库
        Article article = articleMapper.selectByPrimaryKey(id);
        return article;
    }

    @Override
    public void addOneArticleLikeCountById(Long id) {
        //1.参数校验
        if(id == null) {
            log.warn("文章id为空");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //2.根据文章id查询改文章的信息
        Article article = articleMapper.selectByPrimaryKey(id);

        //3.参数校验
        if(article == null) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        //4.根据id更新该片文章的点赞数量
        Article article1 = new Article();
        article1.setId(id);
        article1.setLikeCount(article.getLikeCount() + 1);
        
        //5.调用数据库
        int row = articleMapper.updateByPrimaryKeySelective(article1);

        //6.参数校验
        if(row != 1) {
            log.warn(ResultCode.FAILED.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }

        log.info("点赞数更新成功");
    }

    @Override
    public void subOneArticleLikeCountById(Long id) {
        System.out.println("检查id" + id);
        //1.参数校验
        if(id == null) {
            log.warn("文章id为空");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        //2.根据文章id查询改文章的信息
        Article article = articleMapper.selectByPrimaryKey(id);


        //3.参数校验
        if(article == null) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        //4.根据id更新该片文章的点赞数量
        Article article1 = new Article();
        article1.setId(id);
        article1.setLikeCount(article.getLikeCount() - 1);

        //5.调用数据库
        int row = articleMapper.updateByPrimaryKeySelective(article1);

        //6.参数校验
        if(row != 1) {
            log.warn(ResultCode.FAILED.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }

        log.info("该篇文章点赞数减少成功");

    }

    @Override
    public void deleteById(Long id) {
        //1.参数校验
        if(id == null) {
            log.warn("文章id为空");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        //2.根据文章id查询改文章的信息
        Article article = articleMapper.selectByPrimaryKey(id);


        //3.参数校验
        if(article == null || article.getDeleteState() == 1) {
            log.warn(ResultCode.FAILED_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_NOT_EXISTS));
        }

        //4.根据id更新该片文章的点赞数量
        Article article1 = new Article();
        article1.setId(article.getId());
        article1.setDeleteState((byte) 1);

        //5.更新数据库
        int row = articleMapper.updateByPrimaryKeySelective(article1);

        //6.参数校验
        if(row != 1) {
            log.warn(ResultCode.FAILED.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }

        //7.咨询师文章数量减少
        iConUserService.subOneArticleCountById(article.getUserId());

        log.info("文章删除成功");

    }

    @Override
    public void addOneReplyCountById(Long id) {
        //1.参数校验
        if(id == null) {
            log.warn("文章id为空");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        //2.根据文章id查询出当前的文章信息
        Article article = articleMapper.selectByPrimaryKey(id);

        //3.参数校验
        if(article == null || article.getDeleteState() == 1) {
            log.warn(ResultCode.FAILED_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_NOT_EXISTS));
        }

        //4.更新文章回复数量
        Article article1 = new Article();
        article1.setId(article.getId());
        article1.setReplyCount(article.getReplyCount() + 1);
        article1.setUpdateState(new Date());

        int row = articleMapper.updateByPrimaryKeySelective(article1);

        //5.判断更新操作是否成功
        if(row != 1) {
            log.warn(ResultCode.FAILED.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }

        log.info("文章回复数+1成功");

    }

    @Override
    public List<Article> selectByUserId(Long userId) {

        //1.参数校验
        if(userId == null || userId <= 0) {
            log.warn("用户id为空");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        //2.查询当前用户是否存在
        ConUser conUser = iConUserService.selectByPrimaryKey(userId);

        if(conUser == null || conUser.getDeleteState() == 1) {
            log.warn(ResultCode.FAILED_USER_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_USER_NOT_EXISTS));
        }

        //3.调用数据库
        List<Article> articles = articleMapper.selectByUserId(userId);

        //4.返回数据
        return articles;
    }


}
